package com.example.sps.service.Impl;

import com.example.sps.commons.ServiceCode;
import com.example.sps.commons.ServiceException;
import com.example.sps.mapper.ProductMapper;
import com.example.sps.pojo.dto.*;
import com.example.sps.pojo.entity.*;
import com.example.sps.pojo.vo.*;
import com.example.sps.service.ProductService;
import com.example.sps.utils.AliossUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

import static com.example.sps.commons.Constants.BEGIN_TIMESTAMP;

/**
 * @ProjectName: Sps
 * @Titile: ProductServiceImpl
 * @Author: nency
 * @Description: TODO
 */
@Service
@Slf4j
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageVO parameterPage(CommonPageQueryDTO commonPageQueryDTO) {
        PageHelper.startPage(commonPageQueryDTO.getPage(), commonPageQueryDTO.getPageSize());
        Page<ParameterListVO> page = productMapper.parameterPageQuery(commonPageQueryDTO);
        return new PageVO(page.getTotal(),page.getResult(),page.getPageNum(),page.getPages());
    }

    @Override
    public void updateParameter(ParameterUpdateOrAddDTO parameterUpdateOrAddDTO, EmpToken empToken) {
        Parameter parameter = new Parameter();
        BeanUtils.copyProperties(parameterUpdateOrAddDTO,parameter);
        parameter.setModifyEmpId(empToken.getId());
        parameter.setModifyTime(LocalDateTime.now());
        productMapper.updateParam(parameter);
    }

    @Override
    public void addParameter(ParameterUpdateOrAddDTO parameterUpdateOrAddDTO, EmpToken empToken) {
        Parameter parameter = new Parameter();
        BeanUtils.copyProperties(parameterUpdateOrAddDTO,parameter);
        parameter.setModifyEmpId(empToken.getId());
        parameter.setModifyTime(LocalDateTime.now());
        productMapper.insertParam(parameter);
    }

    @Override
    public void deleteParam(Integer id) {
        Parameter parameter = productMapper.getParamById(id);
        if(parameter.getStatus()==0){
            throw new ServiceException(ServiceCode.ERROR,"启用中不能删除");
        }
        if(productMapper.getParamProductByParamId(id)!=null){
            throw new ServiceException(ServiceCode.ERROR,"该参数关联有商品不能删除");
        }
        productMapper.deleteParamById(id);
    }

    @Override
    public void deleteParamBatch(List<Integer> ids) {
        for(Integer id : ids){
            Parameter parameter = productMapper.getParamById(id);
            if(parameter.getStatus()==0){
                throw new ServiceException(ServiceCode.ERROR,"启用中不能删除");
            }
            if(productMapper.getParamProductByParamId(id)!=null){
                throw new ServiceException(ServiceCode.ERROR,"该参数关联有商品不能删除");
            }
        }
        productMapper.deleteParamBatch(ids);
    }

    @Override
    public void updateParamStatus(ParameterStatusDTO parameterStatusDTO, EmpToken empToken) {
        Parameter parameter = new Parameter();
        parameter.setId(parameterStatusDTO.getId());
        parameter.setStatus(parameterStatusDTO.getStatus());
        parameter.setModifyEmpId(empToken.getId());
        parameter.setModifyTime(LocalDateTime.now());
        productMapper.updateParamStatus(parameter);
    }

    @Override
    public PageVO smClassPage(CommonPageQueryDTO commonPageQueryDTO) {
        PageHelper.startPage(commonPageQueryDTO.getPage(), commonPageQueryDTO.getPageSize());
        Page<SmClassVO> page = productMapper.smClassPageQuery(commonPageQueryDTO);
        return new PageVO(page.getTotal(),page.getResult(),page.getPageNum(),page.getPages());
    }

    @Override
    public void addSmClass(SmClassUpdateOrAddDTO smClassUpdateOrAddDTO, EmpToken empToken) {
        SmClass smClass = new SmClass();
        BeanUtils.copyProperties(smClassUpdateOrAddDTO,smClass);
        smClass.setModifyEmpId(empToken.getId());
        smClass.setModifyTime(LocalDateTime.now());
        productMapper.insertSmClass(smClass);
    }

    @Override
    public void updateSmClass(SmClassUpdateOrAddDTO smClassUpdateOrAddDTO, EmpToken empToken) {
        SmClass smClass = new SmClass();
        BeanUtils.copyProperties(smClassUpdateOrAddDTO,smClass);
        smClass.setModifyEmpId(empToken.getId());
        smClass.setModifyTime(LocalDateTime.now());
        productMapper.updateSmClass(smClass);
    }

    @Override
    public void updateSmClassStatus(SmClassStatusDTO smClassStatusDTO, EmpToken empToken) {
        SmClass smClass = new SmClass();
        BeanUtils.copyProperties(smClassStatusDTO,smClass);
        smClass.setModifyEmpId(empToken.getId());
        smClass.setModifyTime(LocalDateTime.now());
        productMapper.updateSmClassStatus(smClass);
    }


    @Override
    public void deleteSmClass(Integer id) {
        SmClass smClass = productMapper.getSmClassById(id);
        if(smClass!=null && smClass.getStatus()==0){
            throw new ServiceException(ServiceCode.ERROR,"启用中不能删除");
        }
        List<SmProduct> smProductList = productMapper.getSmProductByClassId(id);
        if(!smProductList.isEmpty()){
            throw new ServiceException(ServiceCode.ERROR,"该分类下有商品不能删除");
        }
        productMapper.deleteSmClassById(id);
    }

    @Override
    public void deleteSmclassBatch(List<Integer> ids) {
        for(Integer id : ids){
            SmClass smClass = productMapper.getSmClassById(id);
            if(smClass!=null && smClass.getStatus()==0){
                throw new ServiceException(ServiceCode.ERROR,"批量中的分类启用中不能删除");
            }
            List<SmProduct> smProductList = productMapper.getSmProductByClassId(id);
            if(!smProductList.isEmpty()){
                throw new ServiceException(ServiceCode.ERROR,"批量中的分类下有商品不能删除");
            }
        }
        productMapper.deleteSmClassBatch(ids);
    }

    @Override
    public PageVO smProductPage(SmProductQueryDTO smProductQueryDTO) {
        PageHelper.startPage(smProductQueryDTO.getPage(), smProductQueryDTO.getPageSize());
        Page<SmProductVO> page = productMapper.smProductPageQuery(smProductQueryDTO);
        return new PageVO(page.getTotal(),page.getResult(),page.getPageNum(),page.getPages());
    }

    @Override
    @Transactional
    public void updateSmProduct(SmProductDTO smProductDTO, EmpToken empToken) {
        SmProduct smProduct = new SmProduct();
        BeanUtils.copyProperties(smProductDTO,smProduct);
        smProduct.setModifyEmpId(empToken.getId());
        smProduct.setModifyTime(LocalDateTime.now());
        productMapper.updateSmProduct(smProduct);
        List<ParamProductDTO> paramProductDTOList = smProductDTO.getParamProductDTOList();
        productMapper.deleteParamProductByProductId(smProductDTO.getId());
        for(ParamProductDTO paramProductDTO : paramProductDTOList){
            ParamProduct paramProduct = new ParamProduct();
            BeanUtils.copyProperties(paramProductDTO,paramProduct);
            paramProduct.setProductId(smProductDTO.getId());
            productMapper.insertParamProduct(paramProduct);
        }
    }

    @Override
    public void updateSmProductStatus(SmProductStatusDTO smProductStatusDTO, EmpToken empToken) {
        SmProduct smProduct = new SmProduct();
        BeanUtils.copyProperties(smProductStatusDTO,smProduct);
        smProduct.setModifyEmpId(empToken.getId());
        smProduct.setModifyTime(LocalDateTime.now());
        productMapper.updateSmProductStatus(smProduct);
    }

    @Override
    @Transactional
    public void deleteSmProduct(Long id) {
        SmProduct smProduct = productMapper.getSmProductById(id);
        if(smProduct.getStatus()==0){
            throw new ServiceException(ServiceCode.ERROR,"上架中不能删除！");
        }
        productMapper.deleteSmProductById(id);
        productMapper.deleteParamProductByProductId(id);
    }

    @Override
    @Transactional
    public void deleteSmProductBatch(List<Long> ids) {
        for(Long id:ids){
            SmProduct smProduct = productMapper.getSmProductById(id);
            if(smProduct.getStatus()==0){
                throw new ServiceException(ServiceCode.ERROR,"批量中有商品上架中不能删除！");
            }
        }
        for(Long id:ids){
            productMapper.deleteSmProductById(id);
            productMapper.deleteParamProductByProductId(id);
        }
    }

    @Override
    public PageVO homeClassPage(CommonPageQueryDTO commonPageQueryDTO) {
        PageHelper.startPage(commonPageQueryDTO.getPage(), commonPageQueryDTO.getPageSize());
        Page<HomeClassVO> page = productMapper.homeClassPageQuery(commonPageQueryDTO);
        return new PageVO(page.getTotal(),page.getResult(),page.getPageNum(),page.getPages());
    }

    @Override
    public void addHomeClass(HomeClassUpdateOrAddDTO homeClassUpdateOrAddDTO, EmpToken empToken) {
        HomeClass homeClass = new HomeClass();
        BeanUtils.copyProperties(homeClassUpdateOrAddDTO,homeClass);
        homeClass.setModifyEmpId(empToken.getId());
        homeClass.setModifyTime(LocalDateTime.now());
        productMapper.insertHomeClass(homeClass);
    }

    @Override
    public void updateHomeClass(HomeClassUpdateOrAddDTO homeClassUpdateOrAddDTO, EmpToken empToken) {
        HomeClass homeClass = new HomeClass();
        BeanUtils.copyProperties(homeClassUpdateOrAddDTO,homeClass);
        homeClass.setModifyEmpId(empToken.getId());
        homeClass.setModifyTime(LocalDateTime.now());
        productMapper.updateHomeClass(homeClass);
    }

    @Override
    public void updateHomeClassStatus(HomeClassStatusDTO homeClassStatusDTO, EmpToken empToken) {
        HomeClass homeClass = new HomeClass();
        BeanUtils.copyProperties(homeClassStatusDTO,homeClass);
        homeClass.setModifyEmpId(empToken.getId());
        homeClass.setModifyTime(LocalDateTime.now());
        productMapper.updateHomeClassStatus(homeClass);
    }

    @Override
    public void deleteHomeClass(Integer id) {
        HomeClass homeClass = productMapper.getHomeClassById(id);
        if(homeClass!=null && homeClass.getStatus()==0){
            throw new ServiceException(ServiceCode.ERROR,"启用中不能删除");
        }
        List<HomeProduct> homeProductList = productMapper.getHomeProductByClassId(id);
        if(!homeProductList.isEmpty()){
            throw new ServiceException(ServiceCode.ERROR,"该分类下有商品不能删除");
        }
        productMapper.deleteHomeClassById(id);
    }

    @Override
    public void deleteHomeClassBatch(List<Integer> ids) {
        for(Integer id : ids){
            HomeClass homeClass = productMapper.getHomeClassById(id);
            if(homeClass!=null && homeClass.getStatus()==0){
                throw new ServiceException(ServiceCode.ERROR,"批量中的分类启用中不能删除");
            }
            List<HomeProduct> homeProductList = productMapper.getHomeProductByClassId(id);
            if(!homeProductList.isEmpty()){
                throw new ServiceException(ServiceCode.ERROR,"批量中的分类下有商品不能删除");
            }
        }
        productMapper.deleteHomeClassBatch(ids);
    }

    @Override
    @Transactional
    public void addHomeProduct(HomeProductDTO homeProductDTO, EmpToken empToken) {
        HomeProduct homeProduct = new HomeProduct();
        BeanUtils.copyProperties(homeProductDTO,homeProduct);
        homeProduct.setModifyEmpId(empToken.getId());
        homeProduct.setModifyTime(LocalDateTime.now());
        Long id = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC)-BEGIN_TIMESTAMP;
        homeProduct.setId(id);
        productMapper.insertHomeProduct(homeProduct);
        List<ParamProductDTO> paramProductDTOList = homeProductDTO.getParamProductDTOList();
        for(ParamProductDTO paramProductDTO : paramProductDTOList){
            ParamProduct paramProduct = new ParamProduct();
            BeanUtils.copyProperties(paramProductDTO,paramProduct);
            paramProduct.setProductId(id);
            productMapper.insertParamProduct(paramProduct);
        }
    }

    @Override
    public PageVO homeProductPage(HomeProductQueryDTO homeProductQueryDTO) {
        PageHelper.startPage(homeProductQueryDTO.getPage(), homeProductQueryDTO.getPageSize());
        Page<HomeProductVO> page = productMapper.homeProductPageQuery(homeProductQueryDTO);
        return new PageVO(page.getTotal(),page.getResult(),page.getPageNum(),page.getPages());
    }

    @Override
    @Transactional
    public void updateHomeProduct(HomeProductDTO homeProductDTO, EmpToken empToken) {
        HomeProduct homeProduct = new HomeProduct();
        BeanUtils.copyProperties(homeProductDTO,homeProduct);
        homeProduct.setModifyEmpId(empToken.getId());
        homeProduct.setModifyTime(LocalDateTime.now());
        productMapper.updateHomeProduct(homeProduct);
        List<ParamProductDTO> paramProductDTOList = homeProductDTO.getParamProductDTOList();
        productMapper.deleteParamProductByProductId(homeProductDTO.getId());
        for(ParamProductDTO paramProductDTO : paramProductDTOList){
            ParamProduct paramProduct = new ParamProduct();
            BeanUtils.copyProperties(paramProductDTO,paramProduct);
            paramProduct.setProductId(homeProductDTO.getId());
            productMapper.insertParamProduct(paramProduct);
        }
    }

    @Override
    public void updateHomeProductStatus(HomeProductStatusDTO homeProductStatusDTO, EmpToken empToken) {
        HomeProduct homeProduct = new HomeProduct();
        BeanUtils.copyProperties(homeProductStatusDTO,homeProduct);
        homeProduct.setModifyEmpId(empToken.getId());
        homeProduct.setModifyTime(LocalDateTime.now());
        productMapper.updateHomeProductStatus(homeProduct);
    }

    @Override
    @Transactional
    public void deleteHomeProduct(Long id) {
        HomeProduct homeProduct = productMapper.getHomeProductById(id);
        if(homeProduct.getStatus()==0){
            throw new ServiceException(ServiceCode.ERROR,"上架中不能删除！");
        }
        productMapper.deleteHomeProductById(id);
        productMapper.deleteParamProductByProductId(id);
    }

    @Override
    @Transactional
    public void deleteHomeProductBatch(List<Long> ids) {
        for(Long id:ids){
            HomeProduct homeProduct = productMapper.getHomeProductById(id);
            if(homeProduct.getStatus()==0){
                throw new ServiceException(ServiceCode.ERROR,"批量中有商品上架中不能删除！");
            }
        }
        for(Long id:ids){
            productMapper.deleteHomeProductById(id);
            productMapper.deleteParamProductByProductId(id);
        }
    }

    @Override
    public void addProject(ProjectDTO projectDTO, EmpToken empToken) {
        Project project = new Project();
        BeanUtils.copyProperties(projectDTO,project);
        project.setModifyEmpId(empToken.getId());
        project.setModifyTime(LocalDateTime.now());
        productMapper.insertProject(project);
    }

    @Override
    public PageVO ProjectPage(ProjectQueryDTO projectQueryDTO) {
        PageHelper.startPage(projectQueryDTO.getPage(), projectQueryDTO.getPageSize());
        Page<ProjectVO> page = productMapper.projectPageQuery(projectQueryDTO);
        return new PageVO(page.getTotal(),page.getResult(),page.getPageNum(),page.getPages());
    }

    @Override
    public void updateProject(ProjectDTO projectDTO, EmpToken empToken) {
        Project project = new Project();
        BeanUtils.copyProperties(projectDTO,project);
        project.setModifyEmpId(empToken.getId());
        project.setModifyTime(LocalDateTime.now());
        productMapper.updateProject(project);
    }

    @Override
    public void updateProjectStatus(ProjectStatusDTO projectStatusDTO, EmpToken empToken) {
        Project project = new Project();
        BeanUtils.copyProperties(projectStatusDTO,project);
        project.setModifyEmpId(empToken.getId());
        project.setModifyTime(LocalDateTime.now());
        productMapper.updateProjectStatus(project);
    }

    @Override
    public void deleteProject(Integer id) {
        Project project = productMapper.getProjectById(id);
        if(project.getStatus()==0){
            throw new ServiceException(ServiceCode.ERROR,"启用中不能删除！");
        }
        productMapper.deleteProjectById(id);
    }

    @Override
    public void deleteProjectBatch(List<Integer> ids) {
        for(Integer id:ids){
            Project project = productMapper.getProjectById(id);
            if(project.getStatus()==0){
                throw new ServiceException(ServiceCode.ERROR,"批量中有启用中不能删除！");
            }
        }
        productMapper.deleteProjectBatch(ids);
    }


    @Override
    @Transactional
    public void addSmProduct(SmProductDTO smProductDTO, EmpToken empToken) {
        SmProduct smProduct = new SmProduct();
        BeanUtils.copyProperties(smProductDTO,smProduct);
        smProduct.setModifyEmpId(empToken.getId());
        smProduct.setModifyTime(LocalDateTime.now());
        Long id = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC)-BEGIN_TIMESTAMP;
        smProduct.setId(id);
        productMapper.insertSmProduct(smProduct);
        List<ParamProductDTO> paramProductDTOList = smProductDTO.getParamProductDTOList();
        for(ParamProductDTO paramProductDTO : paramProductDTOList){
            ParamProduct paramProduct = new ParamProduct();
            BeanUtils.copyProperties(paramProductDTO,paramProduct);
            paramProduct.setProductId(id);
            productMapper.insertParamProduct(paramProduct);
        }
    }


}
